<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BNode
 *
 * @author I062261
 */
abstract class BNode extends BEntityWithGroup implements BIContextVariable, BIConfig {

    protected $config = null;
    protected $uiManager = null;
    protected $configManager = null;
    public $name = bentity::String_None;
    public $status = null;
    public $description = null;

    public function __construct($item = NULL, $context = NULL) {
        parent::__construct($item, $context);
        if ($this instanceof BApp || $this instanceof BlynApp)
            $this->initImport();
    }

    public function initImport() {
        $cm = $this->getConfigManager();

        $basePath = $cm->getConfigValue('basePath');
        //$basePath = $this->getAttribute('basePath');
        //default import items
        $imports = array(
            'uiPath' => 'ui',
            'control' => 'ui.controls',
            'screen' => 'ui.screens',
            'screenflow' => 'ui.screenflows',
            'model' => 'ui.models',
            'models' => 'models',
            'config' => 'config',
            'app' => 'app'
        );

        $configImport = $cm->getConfigListIntoStringList('imports');

        if (isset($configImport['basePath']))
            unset($configImport['basePath']);

        if ($configImport != NULL)
            $imports = array_merge($imports, $configImport);

        $imports['basePath'] = $basePath;

        foreach ($imports as $importItem) {
            if (is_string($importItem)) {
                $importPath = $basePath . '.' . $importItem . '.*';
                yii::import($importPath);
            }
        }
    }

    public function addContextVariable($name, $value, $scope = NULL, $type = NULL) {

        if ($this instanceof BSpace)
            $scope = BContextObj::Scope_Space;

        if ($this instanceof BApp)
            $scope = BContextObj::Scope_App;

        if ($this instanceof BOperationFlow)
            $scope = BContextObj::Scope_OperationFlow;

        if ($this instanceof BOperation)
            $scope = BContextObj::Scope_Operation;

        if ($scope == NULL)
            $scope = BContextObj::Scope_CurrentNode;

        $tObj = Blyn::app()->getContextObj();

        return $tObj->addContextVariable($scope, $name, $value);
    }

    public function getContextVariable($name, $scope = NULL, $type = NULL) {

        if ($this instanceof BSpace)
            $scope = BContextObj::Scope_Space;

        if ($this instanceof BApp)
            $scope = BContextObj::Scope_App;

        if ($this instanceof BOperationFlow)
            $scope = BContextObj::Scope_OperationFlow;

        if ($this instanceof BOperation)
            $scope = BContextObj::Scope_Operation;

        if ($scope == NULL)
            $scope = BContextObj::Scope_CurrentNode;

        $tObj = Blyn::app()->getContextObj();

        return $tObj->getContextVariable($scope, $name);

        //return FALSE;
    }

    public function resetContextVariable($scope = NULL, $name = NULL) {

        if ($this instanceof BSpace)
            $scope = BContextObj::Scope_Space;

        if ($this instanceof BApp)
            $scope = BContextObj::Scope_App;

        if ($this instanceof BOperationFlow)
            $scope = BContextObj::Scope_OperationFlow;

        if ($this instanceof BOperation)
            $scope = BContextObj::Scope_Operation;

        if ($scope == NULL)
            $scope = BContextObj::Scope_CurrentNode;

        //only when uinode changed, reset Context variables
        $tObj = Blyn::app()->getContextObj();

        //$tObj->resetContextVariable($scope, $name);
        //below code help to protect content for variable, only when obj changed,
        //reset variables
        if ($name == NULL) {
            $tId = $this->getContextVariable(BContextObj::Current_Id, $scope);
            $myId = $this->getId();
            if ($tId != $myId) {
                $tObj->resetContextVariable($scope);
                //$tObj->resetContextVariable(BContextObj::Scope_AppMeta);
                $this->addContextVariable(BContextObj::Current_Id, $myId, $scope);
            }
        } else {
            $tObj->resetContextVariable($scope, $name);
        }
    }

    /**
     * 
     * @param type $data
     * @return BConfigManager
     */
    public function getConfigManager($data = NULL) {
        if ($this->configManager == NULL) {
            $this->configManager = new BConfigManager($this);
            //set default basePath
            $this->configManager->addConfig('basePath', 'application.application');
        }
        return $this->configManager;
    }

    /**
     * @return BUINode Description
     */
    public abstract function getUIManager($data = NULL);

    public function getCache($name) {

        $preCacheId = get_class($this);

        if ($this->getId() != NULL) {
            $preCacheId.=$this->getId();
        }

        $cacheId = $preCacheId . '_' . $name;

        return yii::app()->cache->get($cacheId);
    }

    public function setCache($id, $value, $expire = 0, $dependency = NULL) {

        $preCacheId = get_class($this);

        if ($this->getId() != NULL) {
            $preCacheId.=$this->getId();
        }

        $cacheId = $preCacheId . '_' . $id;

        return yii::app()->cache->set($cacheId, $value, $expire, $dependency);
    }

}

?>
